package data.join.com.media;

import android.content.Context;

import java.io.IOException;
import java.util.List;

import data.join.com.database.bean.FileNameSync;
import data.join.com.database.bean.FileNameSyncPref;
import data.join.com.database.bean.VideoBean;
import data.join.com.database.bean.VideoDBPref;
import data.join.com.network.RetrofitManager;
import data.join.com.network.gson.FileNameUploadRequest;
import data.join.com.service.MainService;
import data.join.com.utils.BaseThread;
import data.join.com.utils.MyDate;
import data.join.com.utils.MyLog;
import data.join.com.utils.QuickShPref;
import data.join.com.utils.WakeLockUtils;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/9/28.
 */

public class FileNameSyncThread extends BaseThread {
    private static String TAG = FileNameSyncThread.class.getSimpleName();

    private static FileNameSyncThread mInstance;

    private String mImei;

    private Context mContext;

    private FileNameSyncThread(Context context){
        this.mContext = context;
    }

    public static FileNameSyncThread getInstance(Context context){
        if(mInstance == null){
            mInstance = new FileNameSyncThread(context);
        }
        return mInstance;
    }

    @Override
    public void run() {

        mImei = QuickShPref.getInstance().getString(QuickShPref.DeviceImei);

        while (true) {
            if(MainService.isNetWorkConnect(mContext,TAG)) {
                WakeLockUtils.getDefault(mContext).acquireWakeLock(TAG);
                fileNameSync();
                WakeLockUtils.getDefault(mContext).releaseWakeLock(TAG);
            }
            await(TIME_WAIT);
        }
    }

    private void fileNameSync(){
        while(true) {
            List<FileNameSync> list = FileNameSyncPref.getInstance().getEarliestFile();

            MyLog.D(TAG, "run start list.size() = " + (list == null?0:list.size()) );

            if (list != null && list.size() > 0) {
                if (uploadFileList(list)) {
                    deleteList(list);
                } else {
                    MyLog.D(TAG, "uploadFileList Fail,break");
                    break;
                }
            }else{
                MyLog.D(TAG, "No file need sync,break");
                break;
            }
        }
    }

    private boolean uploadFileList(List<FileNameSync> list){
        boolean ret = false;

        FileNameUploadRequest fileNameUploadRequest = new FileNameUploadRequest();

        for(FileNameSync fileNameSync:list) {

            VideoBean videoBean = VideoDBPref.getInstance().getVideoById(fileNameSync.mediaId);

            if (videoBean != null) {
                FileNameUploadRequest.UploadFile uploadFile = new FileNameUploadRequest.UploadFile();

                uploadFile.begin_at = MyDate.getFormatTimeByLong(videoBean.startTime);
                uploadFile.end_at = MyDate.getFormatTimeByLong(videoBean.endTime);
                uploadFile.imei = mImei;
                uploadFile.resource_id = videoBean.id;

                fileNameUploadRequest.video_records.add(uploadFile);
            }
        }

        if(fileNameUploadRequest.video_records.size() > 0) {
            Call<Object> responseCall = RetrofitManager.getApiService().uploadVideoFileName(RetrofitManager.getBodyByJson(fileNameUploadRequest));

            try {
                if (responseCall.execute().isSuccessful()) {
                    ret = true;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            ret = true;
        }

        MyLog.D(TAG, "uploadFileList video_records.size() = " + fileNameUploadRequest.video_records.size() +",ret = "+ret);

        return ret;
    }

    private void deleteList(List<FileNameSync> list){
        for(FileNameSync fileNameSync:list) {
            FileNameSyncPref.getInstance().deleteItem(fileNameSync);
        }
    }
}
